﻿using System.Linq;
using System.Web.Mvc;
using Abp.IdentityFramework;
using Abp.UI;
using Abp.Web.Mvc.Controllers;
using Microsoft.AspNet.Identity;
using Abp.Runtime.Validation;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace FutureApp.Web.Controllers
{
    /// <summary>
    /// Derive all Controllers from this class.
    /// </summary>
    public abstract class FutureAppControllerBase : AbpController
    {
        protected FutureAppControllerBase()
        {
            this.LocalizationSourceName = FutureAppConsts.LocalizationSourceName;
        }

        protected virtual void CheckModelState()
        {
            if (!this.ModelState.IsValid)
            {
                throw new UserFriendlyException(L("FormIsNotValidMessage"));
            }
        }

        protected void CheckErrors(IdentityResult identityResult)
        {
            identityResult.CheckErrors(this.LocalizationManager);
        }

        protected override void OnException(ExceptionContext context)
        {
            base.OnException(context);
        }
    }

    public class ValidationResultComparer : IEqualityComparer<ValidationResult>
    {
        public bool Equals(ValidationResult x, ValidationResult y)
        {
            return x.ErrorMessage == y.ErrorMessage;
        }

        public int GetHashCode(ValidationResult result)
        {
            return result.ErrorMessage.GetHashCode();
        }
    }
}